package club.haomoon.front.controller;


import club.haomoon.core.aop.FreeLogin;
import club.haomoon.core.common.AjaxResult;
import club.haomoon.core.common.PageResult;
import club.haomoon.core.validator.annotation.IDMust;
import club.haomoon.front.config.CmsFrontThreadLocal;
import club.haomoon.front.data.article.ArticleCateData;
import club.haomoon.front.data.article.ArticleDetailData;
import club.haomoon.front.data.article.ArticleListedData;
import club.haomoon.front.service.IArticleService;
import club.haomoon.front.validate.article.ArticleCollectValidate;
import club.haomoon.front.validate.article.ArticleSearchValidate;
import club.haomoon.front.validate.common.PageValidate;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/article")
@Tag(name = "文章管理")
public class ArticleController {

    @Resource
    IArticleService iArticleService;

    @FreeLogin
    @GetMapping("/cate")
    @Operation(summary = "文章分类")
    public AjaxResult<List<ArticleCateData>> category() {
        List<ArticleCateData> list = iArticleService.category();
        return AjaxResult.success(list);
    }

    @FreeLogin
    @GetMapping("/lists")
    @Operation(summary = "文章列表")
    public AjaxResult<PageResult<ArticleListedData>> lists(@Validated PageValidate pageValidate,
                                                           @Validated ArticleSearchValidate searchValidate) {
        Integer userId = CmsFrontThreadLocal.getUserId();

        PageResult<ArticleListedData> list = iArticleService.list(userId, pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @FreeLogin
    @GetMapping("/detail")
    @Operation(summary = "文章详情")
    public AjaxResult<ArticleDetailData> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        Integer userId = CmsFrontThreadLocal.getUserId();

        ArticleDetailData vo = iArticleService.detail(id, userId);
        return AjaxResult.success(vo);
    }

    @GetMapping("/collect")
    @Operation(summary = "收藏列表")
    public AjaxResult<PageResult<club.haomoon.front.data.article.ArticleCollectData>> collect(@Validated PageValidate pageValidate) {
        Integer userId = CmsFrontThreadLocal.getUserId();

        PageResult<club.haomoon.front.data.article.ArticleCollectData> list = iArticleService.collect(pageValidate, userId);
        return AjaxResult.success(list);
    }

    @PostMapping("/addCollect")
    @Operation(summary = "收藏加入")
    public AjaxResult<Object> addCollect(@Validated @RequestBody ArticleCollectValidate collectValidate) {
        Integer articleId = collectValidate.getId();
        Integer userId = CmsFrontThreadLocal.getUserId();

        iArticleService.addCollect(articleId, userId);
        return AjaxResult.success();
    }

    @PostMapping("/cancelCollect")
    @Operation(summary = "收藏取消")
    public AjaxResult<Object> cancelCollect(@Validated @RequestBody ArticleCollectValidate collectValidate) {
        Integer articleId = collectValidate.getId();
        Integer userId = CmsFrontThreadLocal.getUserId();

        iArticleService.cancelCollect(articleId, userId);
        return AjaxResult.success();
    }

}
